import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        CompetitionManager competitionManager = new CompetitionManager();
        RegistrationManager registrationManager = new RegistrationManager();
        Scanner scanner = new Scanner(System.in);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

        while (true) {
            System.out.println("请选择操作：");
            System.out.println("1. 添加赛事");
            System.out.println("2. 删除赛事");
            System.out.println("3. 查询所有赛事");
            System.out.println("4. 选手报名");
            System.out.println("5. 查询指定赛事的报名选手");
            System.out.println("6. 取消选手报名");
            System.out.println("7. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.println("请输入赛事名称：");
                    String competitionName = scanner.nextLine();
                    System.out.println("请输入赛事开始时间（格式：yyyy-MM-dd HH:mm）：");
                    String startTimeStr = scanner.nextLine();
                    LocalDateTime startTime = LocalDateTime.parse(startTimeStr, formatter);
                    System.out.println("请输入赛事结束时间（格式：yyyy-MM-dd HH:mm）：");
                    String endTimeStr = scanner.nextLine();
                    LocalDateTime endTime = LocalDateTime.parse(endTimeStr, formatter);
                    System.out.println("请输入赛事地点：");
                    String competitionLocation = scanner.nextLine();
                    Competition competition = new Competition(competitionName, startTime, endTime, competitionLocation);
                    competitionManager.addCompetition(competition);
                    break;
                case 2:
                    System.out.println("请输入要删除的赛事名称：");
                    String competitionToDelete = scanner.nextLine();
                    competitionManager.deleteCompetition(competitionToDelete);
                    System.out.println("赛事删除成功！");
                    break;
                case 3:
                    List<Competition> allCompetitions = competitionManager.getAllCompetitions();
                    for (Competition comp : allCompetitions) {
                        System.out.println("赛事名称：" + comp.getName() + ", 开始时间：" + comp.getStartTime() +
                                ", 结束时间：" + comp.getEndTime() + ", 地点：" + comp.getLocation());
                    }
                    break;
                case 4:
                    System.out.println("请输入要报名的赛事名称：");
                    String registrationCompetition = scanner.nextLine();
                    System.out.println("请输入选手姓名：");
                    String participantName = scanner.nextLine();
                    System.out.println("请输入选手联系方式：");
                    String participantContact = scanner.nextLine();
                    Participant participant = new Participant(participantName, participantContact);
                    registrationManager.registerParticipant(registrationCompetition, participant);
                    System.out.println("报名成功！");
                    break;
                case 5:
                    System.out.println("请输入要查询的赛事名称：");
                    String queryCompetition = scanner.nextLine();
                    List<Participant> participants = registrationManager.getParticipantsByCompetition(queryCompetition);
                    for (Participant p : participants) {
                        System.out.println("选手姓名：" + p.getName() + ", 联系方式：" + p.getContact());
                    }
                    break;
                case 6:
                    System.out.println("请输入要取消报名的赛事名称：");
                    String cancelCompetition = scanner.nextLine();
                    System.out.println("请输入要取消报名的选手姓名：");
                    String cancelParticipantName = scanner.nextLine();
                    System.out.println("请输入要取消报名的选手联系方式：");
                    String cancelParticipantContact = scanner.nextLine();
                    Participant cancelParticipant = new Participant(cancelParticipantName, cancelParticipantContact);
                    registrationManager.cancelRegistration(cancelCompetition, cancelParticipant);
                    System.out.println("取消报名成功！");
                    break;
                case 7:
                    System.out.println("程序退出。");
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("无效的选择，请重新输入。");
            }
        }
    }
}    